﻿using ApplicationBase.Core.Model;
using IdentityService.Application.Commands;
using IdentityService.Application.QueryService;
using IdentityService.Domain.Aggregates.PermitAggregate;
using IdentityService.Domain.Services;
using Infrastructure.Core.Web.Controller;
using Microsoft.AspNetCore.Mvc;

namespace IdentityService.WebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PermitController : BaseController
    {
        private readonly IPermitQueryService _permitQueryService;
        public PermitController(IPermitQueryService permitQueryService)
            => (_permitQueryService) = (permitQueryService);

        [HttpPost]
        public async Task<ActionResult<CommonResponseModel>> Create(long parentId, string name, string description)
        {
            var result = await Mediator.Send(new CreatePermitCommand(parentId, name, description));

            if (result.Result == CreatePermitResult.Success)
                return Ok(new CommonResponseModel());
            else
                return Ok(new CommonResponseModel(ResponseStatus.Failure, result.Result.ToString()));
        }

        [HttpDelete]
        public async Task<ActionResult<CommonResponseModel>> Delete(long id)
        {
            await Mediator.Send(new DeletePermitCommand(id));
            return Ok(new CommonResponseModel());
        }

        [HttpPut]
        public async Task<ActionResult<CommonResponseModel>> Modify(long id, string name, string description)
        {
            await Mediator.Send(new ModifyPermitCommand(id, name, description));
            return Ok(new CommonResponseModel());
        }

        [HttpGet]
        public async Task<ActionResult<Permit>> GetPermitTree() 
        {
            return Ok(await _permitQueryService.GetPermitTree());
        }
    }
}
